<?php

namespace App\Admin\Controllers\user;

use App\Common\statement\CommonStatement;
use App\Models\User;
use App\Models\user\UserCheck;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\MessageBag;

class UserCheckController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '资质审核';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new UserCheck);

        $grid->model()->latest();

        //去除批量删除操作
        $grid->batchActions(function ($batch) {
            $batch->disableDelete();
        });

        $grid->disableCreateButton();

        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1/3, function ($filter) {

                $filter->where(function ($query) {

                    $query->whereHas('user',function ($query){
                        $query->where('uuid', 'like', "%{$this->input}%");
                    })
                        ->orWhere('id', 'like', "%{$this->input}%");

                },__('Uuid'));

            });

            $filter->column(1/3, function ($filter) {


                $filter->where(function ($query) {

                    $query->whereHas('user',function ($query){
                        $query->where('phone', 'like', "%{$this->input}%")
                            ->orWhere('name', 'like', "%{$this->input}%");
                    });

                },'昵称手机');

            });


            $filter->column(1/3, function ($filter) {
                $filter->equal('status',__('Status'))->select(CommonStatement::base_checks);
                $filter->between('created_at',__('Created at'))->datetime();
            });

        });

        $grid->column('id', __('Id'));
        $grid->column('user_id', __('User id'));
        $grid->column('user.header_img', __('header_img'))
            ->lightbox(['width' => 50, 'height' => 50, 'class' => ['circle', 'thumbnail']]);
        $grid->column('user.name', __('User name'));
        $grid->column('user.phone', __('Phone'));
        $grid->column('imgs', __('Imgs'))->lightbox(['width' => 50, 'height' => 50, 'class' => [ 'thumbnail']]);
        $grid->column('remark', __('审核备注'));
        $grid->column('level', __('Level'))->display(function ($s){
            return CommonStatement::level_text[$s];
        });;
        $grid->column('status', __('Status'))->display(function ($status){
            return CommonStatement::base_checks[$status];
        });
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(UserCheck::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('user_id', __('User id'));
        $show->field('imgs', __('Imgs'));
        $show->field('remark', __('Remark'));
        $show->field('level', __('Level'));
        $show->field('status', __('Status'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new UserCheck);

        $form->display('user_id', __('User id'));
        $form->display('user.name', __('User name'));
        $form->display('user.header_img', __('header_img'))->with(function ($value) {
            return  "<img style='max-width: 150px' src='$value' />";
        });
        $form->multipleImage('imgs', __('Imgs'))->readonly();

        $form->display('level', __('Level'))->with(function ($value) {
            return CommonStatement::level_text[$value];
        });
        $form->select('status', __('Status'))->options(CommonStatement::base_checks);
        $form->text('remark', __('Remark'));

        $form->saving(function (Form $form) {

            if ($form->model()->status != 2){
                $error = new MessageBag([
                    'title'   => '申请已处理,请勿重复操作...',
                ]);
                return back()->with(compact('error'));
            }

        });

        $form->saved(function (Form $form) {

            $changes = $form->model()->getChanges();

            if (isset($changes['status'])){

                if ($changes['status'] == 1){

//                    User::whereId($form->model()->user_id)->update(['level'=>$form->model()->level]);
                    $user = User::findOrFail($form->model()->user_id);
                    if ($user instanceof User);
                    $user->level = $form->model()->level;
                    $user->save();
                    if ($user->superior->cate != 3){ // 用户注册成功后  上级推荐人为普通用户  将上级推荐人改为导购或业务员
                        $user->superior->cate = 3;
                        $user->superior->save();
                    }

                    //审核通过
                    $msg = '恭喜您，提现账号审核已通过';
                }else{
                    $msg = '很遗憾您的提现申请被驳回';

                }

            };

        });

        return $form;
    }
}
